<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2 class="login-title">用户登录</h2>
      <el-form
        ref="loginFormRef"
        :model="formData"
        :rules="rules"
        @keyup.enter="handleSubmit"
      >
        <el-form-item prop="username">
          <el-input
            v-model="formData.username"
            placeholder="请输入用户名"
            :prefix-icon="User"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formData.password"
            :type="showPassword ? 'text' : 'password'"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            :show-password="true"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="login-button"
            :loading="loading"
            @click="handleSubmit"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
      <div class="login-footer">
        <span>没有账号？</span>
        <el-link type="primary" @click="handleFooterClick"> 立即注册 </el-link>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { ElForm, ElMessage } from "element-plus";
import { User, Lock } from "@element-plus/icons-vue";
import type { FormInstance, FormRules } from "element-plus";
import { login } from "@/api/login";
import { useRouter } from "vue-router";

const router = useRouter();

interface LoginFormData {
  username: string;
  password: string;
}

const loginFormRef = ref<FormInstance>();
const formData = ref<LoginFormData>({
  username: "",
  password: "",
});
const loading = ref(false);
const showPassword = ref(false);

const rules: FormRules = {
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    {
      min: 3,
      max: 10,
      message: "用户名长度为 3 到 10 个字符",
      trigger: "blur",
    },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    {
      min: 6,
      max: 16,
      message: "密码长度为 6 到 16 个字符",
      trigger: "blur",
    },
  ],
};

const handleSubmit = () => {
  loginFormRef.value?.validate((valid) => {
    if (valid) {
      loading.value = true;
      // 模拟异步操作
      login({
        username: formData.value.username,
        password: formData.value.password,
      }).then((res) => {
        console.log(res, 'res');
        ElMessage.success("登录成功！");
        router.push({ name: 'fsAddPage' });
      });
    } else {
      ElMessage.warning("请填写正确的用户名和密码");
    }
  });
};

const handleFooterClick = () => {
  ElMessage.info("跳转到注册页面");
};
</script>

<style scoped>
.login-container {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #f5f7fa, #c3cfe2);
}

.login-card {
  width: 400px;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
  color: #303133;
}

.login-button {
  width: 100%;
}

.login-footer {
  text-align: center;
  margin-top: 15px;
  font-size: 14px;
  color: #606266;
}

.login-footer .el-link {
  margin-left: 5px;
}
</style>